<#--  修改话题  集合-->
<@function topicId=url_topicId>
<#--  返回Map<String,Object>类型 -->
    <#assign returnValue = topicRelated_editTopic_collection>
    <#assign topicValue = returnValue['topic']>
<#-- 默认Form方式提交出错时会返回topic对象 -->
    <#if topic != null && topic??>
        <#assign topicValue = topic>
    </#if>
<#--  全局允许提交话题 -->
    <#if returnValue['allowTopic'] == true && topicValue != null && topicValue??>
        <!-- 话题编辑器允许使用标签 -->
        <input type="hidden" id="topicAvailableTag" value="${returnValue['availableTag']}">
        <!-- 用户等级 -->
        <input type="hidden" id="userGradeList" value="${returnValue['userGradeList']}">
        <!-- 文件系统 -->
        <input type="hidden" id="fileSystem" value="${returnValue['fileSystem']}">
        <div class="topic-box">
            <form id="editTopicForm" action="user/control/topic/edit?topicId=${url_topicId}" method="post">
                <#-- 提交后跳转参数  参数必须经过encodeBase64URL编码后才能提交 不填写时默认跳转回当前页 -->
                <input type="hidden" id="jumpUrl" name="jumpUrl" value="">
                <div style="height: 50px;">
                    <div style="float:left;">
                        <input type="text" id="title" name="title" class="formText" size="90"
                               value="${topicValue.title}" alt="标题" title="标题"/>
                        <span id="${identificationNumber}_title_error" class="error"
                              name="error">${(error['title'])!}</span>
                    </div>
                </div>
                <div>
                        <textarea class="textarea" onfocus="" id="topicContent"
                                  name="content">${topicValue.content}</textarea>
                </div>

                <span id="${identificationNumber}_content_error" class="error"
                      name="error">${(error['content'])!}</span>
                <span id="${identificationNumber}_topic_error" class="error" name="error">${error['topic']}</span>
                <div class="form-action">
                    <#-- 获取令牌 -->
                    <input type="hidden" id="token" name="token" value="${token!}">

                    <div class="topicSubmit">
							<span class="submitButton-box">
								<button id="${identificationNumber}_submitForm" type="button" class="submitButton"
                                        onClick="javascript:ajax_sureSubmit('${identificationNumber}')">提交</button>
							</span>
                        <span class="cancelButton-box">
								<button type="button" class="cancelButton"
                                        onClick="javascript:window.location.href='thread?topicId=${url_topicId}'">取消</button>
							</span>
                    </div>
                    <#-- identificationNumber:识别号：用来区别每次请求 -->
                    <#-- 验证码 -->
                    <#if returnValue['captchaKey'] != "">
                        <div class="captchaModule">
                            <div class="captcha_div">
                                <div class="captchaBox">
                                    <input type="hidden" id="${identificationNumber}_captchaKey" name="captchaKey"
                                           value="${(returnValue['captchaKey'])!}">
                                    <input id="${identificationNumber}_captchaValue" name="captchaValue" type="text"
                                           maxLength="5" autocomplete="off" tabindex="3"
                                           onfocus="inputBoxTip_click(this);"
                                           onblur="inputBoxTip_leave('${identificationNumber}',this);"/>
                                    <label class="promptTag" id="${identificationNumber}_captchaValueBoxTip"
                                           for="captchaValue">验证码</label>
                                </div>
                                <a class="captchaImage" href="javascript:void(0);">
                                    <img id="${identificationNumber}_captcha"
                                         src="captcha/${(returnValue['captchaKey'])!}.jpg"
                                         onClick="replaceCaptcha('${identificationNumber}');" tabindex="4"/>
                                </a>
                                <a href="javascript:void(0);" onClick="replaceCaptcha('${identificationNumber}');"
                                   tabindex="5">换一幅</a>
                            </div>
                            <p class="tips">
                                <#-- 验证码错误 -->
                                <span id="${identificationNumber}_captchaValue_error"
                                      name="error">${(error['captchaValue'])!}</span>
                            </p>
                        </div>
                    </#if>
                </div>
                <script type="text/javascript">
                    //点击输入框提示
                    function inputBoxTip_click(obj) {
                        document.getElementById(obj.id + "BoxTip").style.display = "none";
                    }

                    //离开输入框提示
                    function inputBoxTip_leave(identificationNumber, obj) {
                        if (obj.value != "") {
                            document.getElementById(obj.id + "BoxTip").style.display = "none";
                        } else {
                            document.getElementById(obj.id + "BoxTip").style.display = "";
                        }
                        verificationCode(identificationNumber);
                    }

                    //校对验证码 identificationNumber:识别号：用来区别每次请求
                    function verificationCode(identificationNumber) {
                        var flag = true;
                        if (document.getElementById(identificationNumber + "_" + "captchaKey") != null) {
                            var captchaKey = document.getElementById(identificationNumber + "_" + "captchaKey").value;
                            var parameter = document.getElementById(identificationNumber + "_" + "captchaValue").value;
                            if (parameter != "") {
                                var parameter_trim = trim(parameter);
                                if (parameter_trim != "") {
                                    //ajax使用同步提交
                                    get_request(function (value) {
                                        if (value == "false") {
                                            document.getElementById(identificationNumber + "_" + "captchaValue_error").innerHTML = "验证码错误";
                                            flag = false;
                                        }
                                    },
                                        "checkCaptcha.htm?captchaKey=" + captchaKey + "&captchaValue=" + parameter_trim + "&timestamp=" + new Date().getTime(), false);
                                } else {
                                    document.getElementById(identificationNumber + "_" + "captchaValue_error").innerHTML = "验证码不能为空";
                                    flag = false;
                                }
                            } else {
                                document.getElementById(identificationNumber + "_" + "captchaValue_error").innerHTML = "验证码不能为空";
                                flag = false;
                            }
                            if (flag == true) {
                                document.getElementById(identificationNumber + "_" + "captchaValue_error").innerHTML = "";
                            }
                        }
                        return flag;
                    }

                    //更换验证码
                    function replaceCaptcha(identificationNumber) {
                        var captchaKey = document.getElementById(identificationNumber + "_" + "captchaKey").value;
                        document.getElementById(identificationNumber + "_" + "captcha").src = "captcha/" + captchaKey + ".jpg?" + Math.random();
                    }

                    //解决提交按钮的click和富文本的blur事件冲突
                    $(document).ready(function () {
                        $("button[type='button']").mousedown(function (e) {
                            e.preventDefault();
                        });
                    });

                    //表单Form方式提交
                    function sureSubmit(obj) {
                        obj.submit();
                    }

                    //Ajax方式提交
                    function ajax_sureSubmit(identificationNumber) {
                        //按钮设置 disabled="disabled"
                        document.getElementById(identificationNumber + "_submitForm").disabled = true;
                        //清空所有错误提示
                        var error_tag = getElementsByName_pseudo("span", "error");
                        for (var i = 0; i < error_tag.length; i++) {
                            error_tag[i].innerHTML = "";
                        }
                        if (verificationCode() == true) {
                            var parameter = "";
                            //话题Id
                            parameter += "&topicId=" + getUrlParam("topicId");//从URL中获取

                            //获取令牌
                            parameter += "&token=" + document.getElementById("token").value;

                            //获取验证码key
                            var captchaKey = document.getElementById(identificationNumber + "_" + "captchaKey");
                            if (captchaKey != null) {
                                parameter += "&captchaKey=" + captchaKey.value;
                            }

                            //获取验证码value
                            var captchaValue = document.getElementById(identificationNumber + "_" + "captchaValue");
                            if (captchaValue != null) {
                                parameter += "&captchaValue=" + captchaValue.value;
                            }

                            //获取标题
                            var title = document.getElementById("title").value;
                            parameter += "&title=" + encodeURIComponent(title);

                            //获取内容
                            var content = document.getElementById("topicContent").value;
                            parameter += "&content=" + encodeURIComponent(content);


                            //删除第一个&号,防止因为多了&号而出现警告: Parameters: Invalid chunk ignored.信息
                            if (parameter.indexOf("&") == 0) {
                                parameter = parameter.substring(1, parameter.length);
                            }
                            post_request(function (value) {
                                var data = JSON.parse(value);
                                for (var returnValue in data) {

                                    if (returnValue == "success") {
                                        if (data[returnValue] == "true") {
                                            //需引入layer
                                            layer.msg('提交成功,1秒后跳转到话题内容页',
                                                {
                                                    time: 1000 //1秒关闭（如果不配置，默认是1秒）
                                                }, function () {//关闭后的操作
                                                    //跳转
                                                    window.location.href = "thread?topicId=" + getUrlParam("topicId");
                                                }
                                            );

                                        }
                                    } else if (returnValue == "error") {

                                        var error = data[returnValue];
                                        for (var key in error) {
                                            var error_html = document.getElementById(identificationNumber + "_" + key + "_error");

                                            if (error_html != null) {
                                                error_html.innerHTML = error[key];
                                            }

                                        }
                                        //按钮设置 disabled="disabled"
                                        document.getElementById(identificationNumber + "_submitForm").disabled = false;
                                    } else if (returnValue == "captchaKey") {//显示验证码

                                        var captchaKey = data[returnValue];
                                        if (captchaKey != "") {
                                            //设置验证码Key
                                            if (document.getElementById(identificationNumber + "_" + "captchaKey") != null) {

                                                document.getElementById(identificationNumber + "_" + "captchaKey").value = captchaKey;
                                                //设置验证码图片
                                                replaceCaptcha(identificationNumber);
                                            }
                                        }
                                    }
                                }
                            },
                                "user/control/topic/edit?timestamp=" + new Date().getTime(), true, parameter);
                        } else {
                            //按钮设置 disabled="disabled"
                            document.getElementById(identificationNumber + "_submitForm").disabled = false;
                        }
                    }


                    KindEditor.lang({
                        hide: '隐藏'
                    });

                    // 指定编辑器iframe document的CSS数据，用于设置可视化区域的样式。 单冒号(:)用于CSS3伪类，双冒号(::)用于CSS3伪元素。伪元素由双冒号和伪元素名称组成。双冒号是在当前规范中引入的，用于区分伪类和伪元素。但是伪类兼容现存样式，浏览器需要同时支持旧的伪类，比如:first-line、:first-letter、:before、:after等
                    KindEditor.options.cssData = ".ke-content hide {" +
                        "border: 0;" +
                        "border-left: 3px solid #06b5ff;" +
                        "margin-left: 10px;" +
                        "padding: 0.5em;" +
                        "min-height:26px;" +
                        "display: block;" +
                        "margin: 30px 0px 0px 0px;" +
                        "}" +
                        ".ke-content .inputValue_10:before {" +
                        "content: '密码: ' attr(input-value) '';" +
                        " color: #06b5ff;" +
                        "font-size:14px;" +
                        "position: absolute;" +
                        "margin-top: -30px;" +
                        "line-height: 30px;" +
                        "}" +
                        ".ke-content .inputValue_20:before {" +
                        "content: '回复话题可见';" +
                        " color: #06b5ff;" +
                        "font-size:14px;" +
                        "position: absolute;" +
                        "margin-top: -30px;" +
                        "line-height: 30px;" +
                        "}" +
                        ".ke-content .inputValue_30:before {" +
                        "content: '等级达到 ' attr(description) ' 可见';" +
                        " color: #06b5ff;" +
                        "font-size:14px;" +
                        "position: absolute;" +
                        "margin-top: -30px;" +
                        "line-height: 30px;" +
                        "}" +
                        ".ke-content .inputValue_40:before {" +
                        "content: '需要支付 ' attr(input-value) ' 积分可见';" +
                        " color: #06b5ff;" +
                        "font-size:14px;" +
                        "position: absolute;" +
                        "margin-top: -30px;" +
                        "line-height: 30px;" +
                        "}" +
                        ".ke-content .inputValue_50:before {" +
                        "content: '需要支付 ' attr(input-value) ' 元费用可见';" +
                        " color: #06b5ff;" +
                        "font-size:14px;" +
                        "position: absolute;" +
                        "margin-top: -30px;" +
                        "line-height: 30px;" +
                        "}" +//突出编辑框的代码
                        ".ke-content .prettyprint {" +
                        "background:#f8f8f8;" +
                        "border:1px solid #ddd;" +
                        "padding:5px;" +
                        "}" +//默认字体大小
                        "body {" +
                        "font-size: 14px;" +
                        "}";

                    //指定要保留的HTML标记和属性。Object的key为HTML标签名，value为HTML属性数组，”.”开始的属性表示style属性。 注意属性要全部小写
                    KindEditor.options.htmlTags['hide'] = ['hide-type', 'input-value', 'class', 'description'];

                    var topicAvailableTag = document.getElementById("topicAvailableTag").value;
                    var topicAvailableTag_obj = null;//['source', '|','fontname','fontsize','emoticons'];
                    if (topicAvailableTag != "") {
                        var flag = false;
                        topicAvailableTag_obj = JSON.parse(topicAvailableTag);//JSON转为对象
                        if (topicAvailableTag_obj != null && topicAvailableTag_obj.length > 0) {
                            for (var i = 0; i < topicAvailableTag_obj.length; i++) {
                                var availableTag = topicAvailableTag_obj[i];
                                if (availableTag == "hidePassword" || availableTag == "hideComment" ||
                                    availableTag == "hideGrade" || availableTag == "hidePoint" || availableTag == "hideAmount") {
                                    topicAvailableTag_obj.splice(i, 0, 'hide');//在指定索引处插入元素
                                    break;
                                }
                            }
                            for (var i = 0; i < topicAvailableTag_obj.length; i++) {
                                var availableTag = topicAvailableTag_obj[i];
                                if (availableTag == "image") {//增加批量插入图片按钮
                                    topicAvailableTag_obj.splice(i + 1, 0, 'multiimage');//在指定索引处插入元素
                                    break;
                                }
                            }
                        }
                    }

                    //等级
                    var userGradeList = document.getElementById("userGradeList").value;
                    var userGradeList_obj = null;
                    if (userGradeList != "") {
                        userGradeList_obj = JSON.parse(userGradeList);//JSON转为对象
                    }
                    var editor;
                    KindEditor.ready(function (K) {
                        var fileSystem = document.getElementById("fileSystem").value;

                        editor = K.create('textarea[id="topicContent"]', {
                            themeType: 'style :minimalist customization',//极简主题 加冒号的是主题样式文件名称同时也是主题目录
                            basePath: '${commonPath}js/kindeditor/',//指定编辑器的根目录路径
                            formatUploadUrl: false,//false时不会自动格式化上传后的URL
                            resizeType: 1,//2或1或0，2时可以拖动改变宽度和高度，1时只能改变高度，0时不能拖动。默认值: 2
                            autoHeightMode: true,//自适应高度
                            allowPreviewEmoticons: true,//true或false，true时鼠标放在表情上可以预览表情
                            allowImageUpload: true,//true时显示图片上传按钮
                            uploadModule: parseInt(fileSystem),//上传模块 0.本地 10.SeaweedFS 20.MinIO 30.阿里云OSS
                            uploadJson: 'user/control/topic/upload.htm?method=upload',//指定浏览远程图片的服务器端程序
                            filePostName: 'file',//文件上传字段 默认imgFile
                            fixToolBar: true,//浮动工具栏
                            items: topicAvailableTag_obj,

                            /**['source', 'fontsize', 'forecolor', 'hilitecolor', 'bold',
                             'italic', 'underline',  '|', 'image', 'multiimage',
                             'justifyleft','justifycenter','justifyright', 'insertorderedlist', 'insertunorderedlist',   'emoticons',
                             'link', 'unlink','removeformat','hide'],**/
                            userGradeList: userGradeList_obj,
                            afterChange: function () {
                                this.sync();
                            },
                            afterBlur: function () {
                                this.sync();
                            }

                        });
                        //自动展开内容
                        editor.autoExpandContent();
                    });
                </script>

            </form>
        </div>
    </#if>

</@function>